#include "view_others_stopwatch.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_others_device_info.h"
#include "view_common.h"
#include "app_trace.h"
#include "stopwatch.h"
#include "view_others_stopwatch.inc"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#ifdef NO_STOPWATCH_UI
#else
typedef enum __stop_watch_type_t
{
    stop_watch_type_main,
    stop_watch_type_other,
} stop_watch_type_t;
static void _disp_show_stopwatch(uint32_t time, int start_y, stop_watch_type_t type)
{
    int font_width = 0;
    int font_interval = 0;
    int point_interval = 0;
    int point_width = 0;
    int ms_interval = 0;
    int x = 0;
    uint8_t const *const *font_tab = 0;
    uint8_t const *point = 0;
    uint32_t temp32 = time;
    uint8_t temp8 = (temp32 % 1000) / 10;
    int point_y_offset = 0;
    int ms_font_width = 6;
    int ms_num_width = ms_font_width;
    int font_height = 29;
    switch (type)
    {
    case stop_watch_type_main:
        font_width = 12;
        font_interval = 4;
        point_interval = 4;
        point_width = 5;
        ms_interval = 5;
        font_tab = num_12x29_TAB;
        point = num_12x29_colon;
        x = 85;
        break;
    case stop_watch_type_other:
        font_width = 11;
        font_interval = 3;
        point_interval = 3;
        point_width = 5;
        ms_interval = 5;
        font_tab = num_11x26_TAB;
        point = num_11x26_colon;
        x = 86;
        break;
    default:
        return;
    }

    disp_draw_user_image(x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval, start_y, num_6x14_TAB[temp8 / 10]); //毫秒
    disp_draw_user_image(x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval + ms_num_width, start_y, num_6x14_TAB[temp8 % 10]);
    if (type == stop_watch_type_main)
    {
        int refresh_x = x + 0 * font_width + 0 * point_width + 0 * font_interval + 0 * point_interval;
        int refresh_y = start_y;
        int end_x = x + 6 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval + ms_interval + ms_num_width + ms_font_width;
        display_set_update_area(refresh_x, refresh_y, end_x + 1 - refresh_x, font_height); //!< 73, 75,225, 192 200 195
    }
    temp32 /= 1000;
    disp_draw_user_image(x + 4 * font_width + 2 * point_width + 2 * font_interval + 4 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //秒
    disp_draw_user_image(x + 5 * font_width + 2 * point_width + 3 * font_interval + 4 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
    disp_draw_user_image(x + 4 * font_width + 1 * point_width + 2 * font_interval + 3 * point_interval, start_y + point_y_offset, point);

    temp32 /= 60;
    disp_draw_user_image(x + 2 * font_width + 1 * point_width + 1 * font_interval + 2 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //分
    disp_draw_user_image(x + 3 * font_width + 1 * point_width + 2 * font_interval + 2 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
    disp_draw_user_image(x + 2 * font_width + 0 * point_width + 1 * font_interval + 1 * point_interval, start_y + point_y_offset, point);

    temp32 /= 60;
    disp_draw_user_image(x + 0 * font_width + 0 * point_width + 0 * font_interval + 0 * point_interval, start_y, font_tab[(temp32 % 60) / 10]); //时
    disp_draw_user_image(x + 1 * font_width + 0 * point_width + 1 * font_interval + 0 * point_interval, start_y, font_tab[(temp32 % 60) % 10]);
}

void view_others_stopwatch_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = stopwatch_get_catch_time(0);
        disp_ctrl.com_data.i_temp[1] = stopwatch_get_catch_time(1);
        disp_ctrl.com_data.i_temp[2] = stopwatch_get_catch_time(2);
        disp_ctrl.com_data.i_temp[3] = stopwatch_get_catch_time(3);
        disp_ctrl.com_data.i_temp[4] = stopwatch_state();
        // display_set_update_area(199, 168, 150, 32); //!< 73, 75,225, 192 200 195
        disp_ctrl.auto_close_delay = 10 * 1000;
        // disp_ctrl.auto_refresh_time = 22;
        if (disp_ctrl.com_data.i_temp[4] == stop_watch_state_start)
        {
            disp_ctrl.auto_refresh_time = 22;
        }
        else
        {
            disp_ctrl.auto_refresh_time = 10 * 1000;
        }
    }
    int x = 35;
    int y = 37;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_stop_watch_base_35_37);
    for (int i = 0; i < 240; i++)
    {
        // 71 115 160
        disp_draw_point(i, 71, 0x39c7);
        disp_draw_point(i, 115, 0x39c7);
        disp_draw_point(i, 160, 0x39c7);
    }
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[0], 168, stop_watch_type_main);
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[1], 36, stop_watch_type_other);
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[2], 80, stop_watch_type_other);
    _disp_show_stopwatch(disp_ctrl.com_data.i_temp[3], 124, stop_watch_type_other);
}

view_event_result_t view_others_stopwatch_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        // stopwatch_reset();
        return view_event_result_handled;
    case event_view_exit:
        stopwatch_stop();
        return view_event_result_handled;
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            stopwatch_catch_time();
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
            return view_event_result_handled;
        }
    }
    break;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
		{
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            return view_event_result_handled;
        }
        break;
			}
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
    {
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            stopwatch_stop();
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
        }
        else
        {
            stopwatch_resume();
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
        }
        return view_event_result_handled;
    }    
#endif
#ifdef EVENT_LEVEL_DOWN
    case BUTTON_EVENT_LONG_PRESS:
        switch (param->source)
        {
        case event_source_button_enter:
        {
            stop_watch_state_t state = stopwatch_state();
            if (state == stop_watch_state_start)
            {
                stopwatch_stop();
                display_set_update_request(view_others_stopwatch, 0);
                disp_ctrl.area_update_flag = 0;
            }
            else
            {
                stopwatch_start();
                display_set_update_request(view_others_stopwatch, 0);
                disp_ctrl.area_update_flag = 0;
            }
            return view_event_result_handled;
        }
        }
        break;
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
    {
        stop_watch_state_t state = stopwatch_state();
        if (state == stop_watch_state_start)
        {
            stopwatch_stop();
            // disp_ctrl.auto_refresh_time = 22;
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
        }
        else
        {
            stopwatch_start();
            // disp_ctrl.auto_refresh_time = 22;
            display_set_update_request(view_others_stopwatch, 0);
            disp_ctrl.area_update_flag = 0;
        }
        return view_event_result_handled;
    }
    }
    return view_event_result_not_handled;
}
#endif
